﻿using System;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.IO;

public partial class site_log_EditFile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindData();
        }
    }

    private void bindData()
    {
        if (Request.QueryString["path"] != null && Request.QueryString["fn"] != null)
        {
            string path = Request.QueryString["path"].Trim();
            string filename = Request.QueryString["fn"].Trim();
            litCurrentFile.Text = filename;

            string dir = FtpServer.MapPath(path);
            string physicalPath = Path.Combine(dir, filename);

            try
            {
                System.Text.Encoding encoding = EncodingType.GetType(physicalPath);

                FileStream fs = new FileStream(physicalPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

                StreamReader sr = new StreamReader(fs, encoding);
                
                divHTML.InnerHtml = sr.ReadToEnd();

                sr.Close();
                sr.Dispose();

                fs.Close();
                fs.Dispose();

            }
            catch (Exception ex)
            {
                GMS.Core.Log.Log4NetHelper.Error(string.Format("打开文件{0}失败", physicalPath), ex);

                Alert("文件打开失败!");
            }
        }
    }

    protected void Alert(string message)
    {
        string script = "<script type=\"text/javascript\">alert(\"{0}\");</script>";
        ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", string.Format(script, message));
    }

    protected void Close()
    {
        string script = "<script type=\"text/javascript\">window.close();</script>";
        ClientScript.RegisterClientScriptBlock(this.GetType(), "close", script);
    }

}